package com.zhiyou100.login;

import com.zhiyou100.dao.TeacherDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @packageName: ${NAME}
 * @className: ${PACKAGE_NAME}
 * @Description: TODO 判断用户名是否存在！！
 * @author: yang
 * @date: 7/24/20
 */
@WebServlet(name = "TheUserNameExists", urlPatterns = "/ajax/userNameExists")
public class TheUserNameExists extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        // 设定编码集
        response.setContentType("text/html;charset=UTF-8");
        // 设定类型

        // 获取 sname
        String sname = request.getParameter("sname");
        // 判断是否存在该 用户名

        int theIDByName = new TeacherDao().getTheIDByName(sname);
        // 记录信息
        String message = "";
        if (theIDByName == -1) {
            message = "该用户名不存在！";
        } else {
            message = "用户名存在！";
        }
        response.getWriter().write(message);


    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
